{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python3入门到精通实战特训（第19期）第6课书面作业\n",
    "\n",
    "学号：116646\n",
    "\n",
    "**作业内容：**\n",
    "\n",
    "编写一个可以捕获指定异常的装饰器，默认捕获Exception异常。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "运行错误: ZeroDivisionError('division by zero')\n",
      "10/1=10.0\n",
      "10/2=5.0\n",
      "10/3=3.3333333333333335\n",
      "10/4=2.5\n",
      "10/5=2.0\n",
      "10/6=1.6666666666666667\n",
      "10/7=1.4285714285714286\n",
      "10/8=1.25\n",
      "10/9=1.1111111111111112\n"
     ]
    }
   ],
   "source": [
    "def exception_catcher(func):\n",
    "    def wrapper(*args, **kwargs):\n",
    "        try:\n",
    "            result = func(*args, **kwargs)\n",
    "        except Exception as e:\n",
    "            print(\"运行错误: \" + str(repr(e)))\n",
    "            return\n",
    "        return result\n",
    "    return wrapper\n",
    " \n",
    "@exception_catcher\n",
    "def div_demo(a, b):\n",
    "    print('{}/{}={}'.format(a,b,a/b))\n",
    " \n",
    "x = 10\n",
    "for y in range(10):\n",
    "    div_demo(x,y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.2 (heads/master:f26c15e, Oct 21 2022, 16:46:34) [MSC v.1929 64 bit (AMD64)]"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  },
  "vscode": {
   "interpreter": {
    "hash": "1b43cb0bd93d5abbadd54afed8252f711d4681fe6223ad6b67ffaee289648f85"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
